﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;

namespace FBGraph.Configuration
{
    /// <summary>An implementation of <see cref="IEnumerator{TItem}" /> that allows strongly typed
    /// enumeration of <see cref="ConfigurationElementCollection" /> objects.</summary>
    /// <typeparam name="T">The type of <see cref="ConfigurationElement" /> contained by the <see cref="ConfigurationElementCollection" />.</typeparam>
    internal class ConfigurationElementEnumerator<T> : IEnumerator<T>
        where T : ConfigurationElement
    {
        private IEnumerator baseEnumerator;

        /// <summary>Initializes an instance of <see cref="ConfigurationElementEnumerator{TItem}" />.</summary>
        /// <param name="collection">The collection that will be enumerated.</param>
        public ConfigurationElementEnumerator(IEnumerable<T> collection)
        {
            baseEnumerator = ((IEnumerable)collection).GetEnumerator();
        }

        #region [ IEnumerator<ConfigurationElement> Members ]

        /// <summary>Gets the current object in the collection.</summary>
        public T Current
        {
            get { return (T)baseEnumerator.Current; }
        }

        #endregion

        #region [ IDisposable Members ]

        void IDisposable.Dispose() { }

        #endregion

        #region [ IEnumerator Members ]

        Object IEnumerator.Current
        {
            get { return baseEnumerator.Current; }
        }

        /// <summary>Advances the enumerator to the next element of the collection.</summary>
        /// <returns><c>true</c> if the enumerator was successfully advanced to the next element; <c>false</c>
        /// if the enumerator has passed the end of the collection.</returns>
        public Boolean MoveNext()
        {
            return baseEnumerator.MoveNext();
        }

        /// <summary>Sets the enumerator to its initial position, which is before the first element in the collection.</summary>
        public void Reset()
        {
            baseEnumerator.Reset();
        }

        #endregion
    }
}